<?php


namespace app\repository;


use app\model\RoomDevice;
use support\Model;

class RoomRepository extends Repository
{

    public function add($data)
    {
        $model = RoomDevice::query()
            ->where("name", $data['name'])
            ->orWhere("udp_ip",$data['udp_ip'])
            ->orWhere("tcp_ip", $data['tcp_ip'])
            ->orWhere("outside_pc_ip", $data['outside_pc_ip'])
            ->orWhere("inside_pc_ip", $data['inside_pc_ip'])
            ->first();
        if ($model){
            return $this->error("您填写的信息重复，请核实");
        }
        $room = RoomDevice::query()->create($data);
        $room->author = "room_".$room->id;
        return $this->ok();
    }


    public function edit($data)
    {
        $model = RoomDevice::query()->where("id", $data['id'])->first();
        if (!$model){
            return $this->error("您选择的设备不存在");
        }
        $model->update($data);
        return $this->ok();
    }

    public function delete($id)
    {
        $model = RoomDevice::query()->where("id", $id)->first();
        if (!$model){
            return $this->error("您选择的设备不存在");
        }
        $model->delete();
        return $this->ok();
    }


    public function test($id)
    {

    }

    public function list()
    {
        $collection = RoomDevice::query()->get();
        return $this->success($collection);
    }

}